package com.qyer.log.job.sort;

 
import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
 
public class LogCompress
{
    // 自定义map
    public static class Map extends Mapper<LongWritable, Text, Text, NullWritable>
    {
        
        Text val = new Text();
        Text kval = new Text();
        
        public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException
        {
        	kval.set(value.toString());
            context.write(kval, NullWritable.get());
        }
    }
    // 自定义reduce
    //
    public static class Reduce extends Reducer<Text, Text, Text, NullWritable>
    {
        Text t = new Text(); 
        public void reduce(Text key, Iterable<Text> values,Context context) throws IOException, InterruptedException
        {	
        	context.write(key, NullWritable.get());
        }
    }
 
    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException
    {
    	System.out.println("========================== ");
    	for (String arg : args) {
    		
			System.out.println("arg : " +arg);
			
		}
    	System.out.println("========================== ");
        // 读取hadoop配置
        Configuration conf = new Configuration();
//        conf.set("mapred.job.queue.name", "regular"); // default,regular,realtime
        conf.set("mapred.output.compress", "true");
        conf.set("mapred.output.compression.codec", "lzo");
        
        // 实例化一道作业
        Job job = new Job(conf, "LogCompress");
        job.setJarByClass(LogCompress.class);
        // Mapper类型
        job.setMapperClass(Map.class);
        // 不再需要Combiner类型，因为Combiner的输出类型<Text, IntWritable>对Reduce的输入类型<IntPair, IntWritable>不适用
        //job.setCombinerClass(Reduce.class);
        // Reducer类型
        job.setReducerClass(Reduce.class);
 
        // map 输出Key的类型
        job.setMapOutputKeyClass(Text.class);
        // map输出Value的类型
        job.setMapOutputValueClass(NullWritable.class);
        // rduce输出Key的类型，是Text，因为使用的OutputFormatClass是TextOutputFormat
        job.setOutputKeyClass(Text.class);
        // rduce输出Value的类型
        job.setOutputValueClass(NullWritable.class);
 
        // 将输入的数据集分割成小数据块splites，同时提供一个RecordReder的实现。
        job.setInputFormatClass(TextInputFormat.class);
        // 提供一个RecordWriter的实现，负责数据输出。
        job.setOutputFormatClass(TextOutputFormat.class);
        
        job.setNumReduceTasks(1);
 
        // 输入hdfs路径
        FileInputFormat.setInputPaths(job, new Path(args[0]));
        // 输出hdfs路径
        FileSystem.get(conf).delete(new Path(args[1]), true);
        FileOutputFormat.setOutputPath(job, new Path(args[1]));
        
        //启用压缩
        FileOutputFormat.setCompressOutput(job, true);
        //lzo格式
        //FileOutputFormat.setOutputCompressorClass(job, LzopCodec.class);
        //LzoIndexer lzoIndexer = new LzoIndexer(conf);
        //lzoIndexer.index(new Path(args[1]));
        
        //设置压缩格式  bzip2
        FileOutputFormat.setOutputCompressorClass(job, org.apache.hadoop.io.compress.BZip2Codec.class); 
        // 提交job
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}